package com.longxiao.manager.web;

import com.longxiao.exception.LXException;
import com.longxiao.para.AddUserParam;
import com.longxiao.para.UserSearchPara;
import com.longxiao.service.UserService;
import com.longxiao.service.util.MD5Util;
import com.longxiao.vo.UpdateUserVo;
import com.longxiao.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yusong on 2017/6/2.
 * 用户管理
 */
@Controller
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
    *分页搜索用户
    */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute("pageParam") UserSearchPara userSearchPara, HttpServletRequest request) {
        List<UserVo> records = userService.listUserByPage(userSearchPara);
        Integer count = userService.count(userSearchPara);
        userSearchPara.setTotalCount(count);
        request.setAttribute("pageParam", userSearchPara);
        request.setAttribute("records", records);
        return "user/list";
    }

    /*
   *开始新增用户
   *跳转到新增用户页面
   */
    @RequestMapping(value = "/addBefore")
    private String addBefore() {
        return "user/add";
    }

    /**
     * ajax新增用戶
     */
    @RequestMapping(value = "/doAdd")
    @ResponseBody
    public Map<String, String> doAdd(@ModelAttribute("property") AddUserParam addUserParam) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String message = "";
        boolean result = false;
        try {
            userService.addTempUser(addUserParam);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }

    /*
   *开始新增用户
   *跳转到新增用户页面
   */
    @RequestMapping(value = "/updateBefore/{userId}")
    private String updateBefore(@PathVariable("userId") String userId, HttpServletRequest request) {
        UserVo userVo = null;
        try {
            userVo = userService.getUserInfo(userId);
        } catch (LXException e) {
            e.printStackTrace();
        }
        request.setAttribute("record", userVo);
        return "user/update";
    }

    /*
   *新增版本
   */
    @RequestMapping(value = "/doUpdate")
    @ResponseBody
    public Map<String, String> doUpdate(@ModelAttribute("property") UpdateUserVo updateUserVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtils.isEmpty(updateUserVo.getBirthdayStr())){
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                updateUserVo.setBirthday(format.parse(updateUserVo.getBirthdayStr()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String password = updateUserVo.getPassword();
        if(!StringUtils.isEmpty(password)){
            try {
                updateUserVo.setPassword(MD5Util.md5(password));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String message = "";
        boolean result = false;
        try {
            userService.updateUser(updateUserVo);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("statusCode", "300");
            message = "操作失败";
        }
        if (result) {
            resultMap.put("statusCode", "200");
            message = "操作成功";
        }
        resultMap.put("message", message);
        resultMap.put("callbackType", "closeCurrent");
        return resultMap;
    }

}
